package maiyatian.videolive;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.activity.SearchActivity;
import maiyatian.videolive.fragment.MainFragment;
import maiyatian.videolive.fragment.PersonLeftFragment;
import maiyatian.videolive.fragment.focus.FocusFragment;
import maiyatian.videolive.fragment.newest.NewestFragment;

// TODO: 2016/5/11 tablayout默认热门选中（第二个）

@ContentView(R.layout.activity_main)
public class MainActivity extends IActivity implements TabLayout.OnTabSelectedListener {
    public static String TAG = "MainActivity";
    private Fragment personleftFragment, mainFragment, focusFragment, newestFragment;
    private List<Fragment> fragments;
    private List<String> tabTitles;
    @ViewInject(R.id.drawer)
    DrawerLayout drawerLayout;
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initFrags();
        comFrags();
        initTablayout();
    }

    private void initTablayout() {
        tabLayout = (TabLayout) this.findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpagerHome);
        mainFragment = new MainFragment();
        focusFragment = new FocusFragment();
        newestFragment = new NewestFragment(); // TODO: 2016/5/11
//        newestFragment = new NewestTopicFragment();
        fragments = new ArrayList<>();
        tabTitles = new ArrayList<>();
        fragments.add(focusFragment);
        fragments.add(mainFragment);
        fragments.add(newestFragment);
        tabTitles.add(getResources().getString(R.string.hot));
        tabTitles.add(getResources().getString(R.string.lastNew));
        tabTitles.add(getResources().getString(R.string.focus));
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(tabTitles.get(2)));
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, this);
        viewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
        tabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
        if (viewPager != null) {
            setupViewPager(viewPager);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setOnTabSelectedListener(this);
        }


    }

    private void setupViewPager(ViewPager viewPager) {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        focusFragment = new FocusFragment();
        fragmentAdapter.addFragment(focusFragment, getString(R.string.focus));
        mainFragment = new MainFragment();
        fragmentAdapter.addFragment(mainFragment, getString(R.string.hot));
        newestFragment = new NewestFragment(); // TODO: 2016/5/11
//        newestFragment = new NewestTopicFragment();
        fragmentAdapter.addFragment(newestFragment, getString(R.string.lastNew));
        viewPager.setAdapter(fragmentAdapter);
    }

    private void comFrags() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frameLeftBox, personleftFragment)
                .commit();
    }

    private void initFrags() {
        personleftFragment = new PersonLeftFragment();
    }

    //search
    public void searchClick(View v) {
        startActivity(new Intent(this, SearchActivity.class));
    }

    public void closeLeftFragment() {
        drawerLayout.closeDrawer(Gravity.LEFT);
    }

    public void menuClick(View v) {
        Log.e(TAG, "menuClick: ");
        drawerLayout.openDrawer(Gravity.LEFT);
    }

    //tablayout选中状态
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int pos = tab.getPosition();
        viewPager.setCurrentItem(pos, true);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    //切换
    static class FragmentAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

    //适配器
    class MyPagerAdapter extends FragmentStatePagerAdapter

    {
        private MainActivity activity;
        private List<Fragment> datas;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> datas, MainActivity activity) {
            super(fm);
            this.activity = activity;
            this.datas = datas;
        }

        @Override
        public Fragment getItem(int position) {
            return datas.get(position);
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles.get(position);//页卡标题
        }

        ////添加页卡 初始化item
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

    }
}
